//
//  XLNewTopUpView.m
//  XiangLove
//
//  Created by feng song on 2017/11/30.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLNewTopUpView.h"
#import "WeixinHelper.h"

@implementation XLNewTopUpView

+(XLNewTopUpView *)shareXLNewZHIFU
{
    static XLNewTopUpView * newTouUpView = nil;
    if (newTouUpView == nil) {
        newTouUpView = [[XLNewTopUpView alloc] init];
    }
    return newTouUpView;
    
}


-(instancetype)initWithFrame:(CGRect)frame withIn_purchase:(BOOL)In_purchase withTopupMonyAry:(NSMutableArray *)monyAry withTopupJDAry:(NSMutableArray *)jdAry
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.In_purchase = In_purchase;
        self.topupMoneyAry = monyAry;
        self.topupJDAry    = jdAry;
        self.money = @"30";
        [self addUIview];
        
    }
    return self;
}

- (NSMutableArray *)btnArray {
    if (!_btnArray) {
        NSMutableArray *array = [NSMutableArray array];
        _btnArray = array;
        
    }
    return _btnArray;
}

-(void)addUIview
{
    self.backgroundColor = [UIColor clearColor];
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    [self addSubview:bgView];
    
    UIView * backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.layer.masksToBounds = YES;
    backgroundView.layer.cornerRadius = 10;
    self.bgWidth = backgroundView.frame.size.width;
    self.bgHeigh = backgroundView.frame.size.height;
    [bgView addSubview:backgroundView];
    
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(270, 192));
    }];
    
    
    
    UILabel * topUpTitle = [[UILabel alloc] init];
    topUpTitle.text = @"金豆不足 请先充值";
    topUpTitle.font = [UIFont systemFontOfSize:17.0];
    topUpTitle.textColor = RGB_HEX(0x111111);
    topUpTitle.textAlignment = NSTextAlignmentCenter;
    [backgroundView addSubview:topUpTitle];
    
    [topUpTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backgroundView.mas_top).offset(10);
        make.height.equalTo(@24);
        make.centerX.equalTo(backgroundView.mas_centerX);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"play_pop_icon_chacha_n"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(handelCloseAction:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:closeBtn];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.top.equalTo(backgroundView.mas_top).offset(15);
        make.right.equalTo(backgroundView.mas_right).offset(-15);
    }];
    
    UIView * firstMoneyView = [[UIView alloc]init];
    ViewBorderRadius(firstMoneyView, 3, 0.5, RGB_HEX(0xFFBB01));
    firstMoneyView.tag = 101;
    [backgroundView addSubview:firstMoneyView];
    
    UIView * secondMoneyView = [[UIView alloc]init];
    ViewBorderRadius(secondMoneyView, 3, 0.5, RGB_HEX(0xCCCCCC));
    secondMoneyView.tag = 201;
    [backgroundView addSubview:secondMoneyView];
    
    [firstMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView.mas_left).offset(16);
        make.top.equalTo(backgroundView.mas_top).offset(60);
        make.height.equalTo(@64);
        make.width.equalTo(secondMoneyView.mas_width);
        make.right.equalTo(secondMoneyView.mas_left).offset(-16);
    }];
    
    [secondMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView.mas_right).offset(-16);
        make.top.equalTo(backgroundView.mas_top).offset(60);
        make.height.equalTo(@64);
        make.width.equalTo(firstMoneyView.mas_width);
        make.left.equalTo(firstMoneyView.mas_right).offset(16);
    }];
    
    for(int i = 0; i < 2; i ++)
    {
        UIImageView * checkIcon = [[UIImageView alloc]init];
        checkIcon.hidden = YES;
        checkIcon.image = IMAGE_NAMED(@"check");
        UILabel * moneylabelTop = [[UILabel alloc]init];
        moneylabelTop.font = SYSTEMFONT(15);
        UILabel * moneylabelBottom = [[UILabel alloc]init];
        moneylabelBottom.font = SYSTEMFONT(15);
        UIButton * btn = [[UIButton alloc]init];
        [btn addTarget:self action:@selector(chooseMoney:) forControlEvents:UIControlEventTouchUpInside];
        UIView * view;
        if(i == 0)
        {
            //firstView
            checkIcon.tag = 102;
            checkIcon.hidden = NO;
            [firstMoneyView addSubview:checkIcon];
            moneylabelTop.textColor = RGB_HEX(0xFFBB01);
            moneylabelTop.tag = 103;
            moneylabelTop.text = @"300个金豆";
            [firstMoneyView addSubview:moneylabelTop];
            
            moneylabelBottom.textColor = RGB_HEX(0xFFBB01);
            moneylabelBottom.tag = 104;
            moneylabelBottom.text = @"30元";
            [firstMoneyView addSubview:moneylabelBottom];
            
            btn.tag = 105;
            [firstMoneyView addSubview:btn];
            view = firstMoneyView;
        }else
        {
            //secondView
            checkIcon.tag = 202;
            checkIcon.hidden = YES;
            [secondMoneyView addSubview:checkIcon];
            moneylabelTop.textColor = RGB_HEX(0x666666);
            moneylabelTop.tag = 203;
            moneylabelTop.text = @"990个金豆";
            [secondMoneyView addSubview:moneylabelTop];
            
            moneylabelBottom.textColor = RGB_HEX(0x666666);
            moneylabelBottom.tag = 204;
            moneylabelBottom.text = @"99元";
            [secondMoneyView addSubview:moneylabelBottom];
            
            btn.tag = 205;
            [secondMoneyView addSubview:btn];
            view = secondMoneyView;
        }
        
        [checkIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top).offset(0);
            make.right.equalTo(view.mas_right).offset(0);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        
        [moneylabelTop mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@15);
            make.top.equalTo(view.mas_top).offset(12);
            make.centerX.equalTo(view.mas_centerX);
        }];
        
        [moneylabelBottom mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@15);
            make.top.equalTo(moneylabelTop.mas_bottom).offset(10);
            make.centerX.equalTo(view.mas_centerX);
        }];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(view);
        }];
    }
    
    UIButton * xlWXZFBtn = [[UIButton alloc] init];
    xlWXZFBtn.backgroundColor = RGB_HEX(0x42C570);
    xlWXZFBtn.layer.masksToBounds = YES;
    xlWXZFBtn.layer.cornerRadius = 4;
    [xlWXZFBtn setTitle:@"微信支付" forState:UIControlStateNormal];
    [xlWXZFBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [xlWXZFBtn setImage:[UIImage imageNamed:@"logo_weixin"] forState:UIControlStateNormal];
//    [xlWXZFBtn addTarget:self action:@selector(xlwxZFAction:) forControlEvents:UIControlEventTouchUpInside];
    kWeakSelf(self);
    [xlWXZFBtn addTapBlock:^(UIButton *btn) {
        [[WeixinHelper sharedWeixinHelper] getUrlWithWXMoney:self.money Months:@"0" TranstypeId:@"1" RemarksInfo:RemarksRose KKOrder:@"" kkUserid:@""];
        weakself.hidden = YES;
        [weakself removeFromSuperview];
    }];
    [backgroundView addSubview:xlWXZFBtn];
    
    [xlWXZFBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(firstMoneyView.mas_bottom).offset(16);
        make.left.equalTo(backgroundView.mas_left).offset(16);
        make.right.equalTo(backgroundView.mas_right).offset(-15);
        make.height.equalTo(@40);
    }];
    
//
//
//
//
//    UIView * selecedView = [[UIView alloc] initWithFrame:CGRectMake(0,60, self.bgWidth, 150)];
//    selecedView.backgroundColor = kWhiteColor;
//    [backgroundView addSubview:selecedView];
//    self.selecedView = selecedView;
//
//    CGFloat xj = (self.bgWidth-130*2)/3;
//    CGFloat yj = (150-60*2)/3;
//
//    NSArray * moneyAry = [NSMutableArray  array];
//    NSArray * jdAry    = [NSMutableArray  array];
//    if ([[UserDefaults objectForKey:XiangLove_IsSH] boolValue] == YES) {
//        moneyAry = @[@"42",@"210",@"350",@"756"];
//        jdAry    = @[@"6",@"30",@"50",@"108"];
//    }else{
//
//        moneyAry = self.topupJDAry;
//        jdAry    = self.topupMoneyAry;
//
//    }
//
//
//
//
//    for (int i = 0; i<4; i++) {
//
//        UIButton * jDBtn = [[UIButton alloc] initWithFrame:CGRectMake(xj+i%2*(xj+130), yj+i/2*(yj+60), 130, 60)];
//        jDBtn.backgroundColor = kWhiteColor;
//        jDBtn.tag = i;
//        [jDBtn setBackgroundImage:[UIImage imageNamed:@"conten_but_bk_default"] forState:UIControlStateNormal];
//        [jDBtn setBackgroundImage:[UIImage imageNamed:@"conten_icon_bk_hig"] forState:UIControlStateSelected];
//        [jDBtn addTarget:self action:@selector(selecttedJDAction:) forControlEvents:UIControlEventTouchUpInside];
//        jDBtn.userInteractionEnabled = YES;
//        [selecedView addSubview:jDBtn];
//
//        UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 12,130, 20)];
//        titleView.tag = 10+i;
//        titleView.text = [NSString stringWithFormat:@"%@金豆",[moneyAry objectAtIndex:i]];
//        titleView.textColor = RGB_HEX(0x272727);
//        titleView.font = [UIFont systemFontOfSize:14.0];
//        titleView.textAlignment = NSTextAlignmentCenter;
//        titleView.userInteractionEnabled = NO;
//        [jDBtn addSubview:titleView];
//        self.titleView = titleView;
//
//
//        UILabel * jdView = [[UILabel alloc] initWithFrame:CGRectMake(0,32,130, 17)];
//        jdView.tag = 20+i;
//        jdView.text = [NSString stringWithFormat:@"%@元",[jdAry objectAtIndex:i]];
//        jdView.textColor = RGB_HEX(0x999999);
//        jdView.font = [UIFont systemFontOfSize:12.0];
//        jdView.textAlignment = NSTextAlignmentCenter;
//        jdView.userInteractionEnabled = NO;
//        [jDBtn addSubview:jdView];
//        self.jdView = jdView;
//
//        [self.btnArray addObject:jDBtn];
//
//        if (i == 0) {
//            jDBtn.selected = YES;
//            self.titleView.textColor = RGB_HEX(0xff4a45);
//            self.jdView.textColor    = RGB_HEX(0xff4a45);
//            self.Selectbutton = jDBtn;
//        }
//
//
//    }
//
//    UIView * bottomView = [[UIView alloc] initWithFrame:CGRectMake(0,268-58,self.bgWidth, 58)];
//    bottomView.backgroundColor = kWhiteColor;
//    [backgroundView addSubview:bottomView];
//
//    if ([[UserDefaults objectForKey:XiangLove_IsSH] boolValue] == YES) {
//
//        UIButton * payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        payBtn.frame = CGRectMake(18,(58-36)/2,self.bgWidth-18*2, 36);
//        payBtn.backgroundColor = HEXCOLOR(0xff4a45);
//        [payBtn setTitle:@"立即充值" forState:UIControlStateNormal];
//        [payBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
//        payBtn.titleLabel.font = kSetFont(15.0);
//        payBtn.layer.masksToBounds = YES;
//        payBtn.layer.cornerRadius = 5;
//        payBtn.layer.borderWidth = 1.0;
//        payBtn.layer.borderColor = RGB_HEX(0xcbc9c9).CGColor;
//        [payBtn addTarget:self action:@selector(payBtn:) forControlEvents:UIControlEventTouchUpInside];
//        [bottomView addSubview:payBtn];
//
//    }else{
//
//        UIButton * xlWXZFBtn = [[UIButton alloc] initWithFrame:CGRectMake(18,(58-36)/2, 272, 36)];
//        xlWXZFBtn.backgroundColor = RGB_HEX(0x18d100);
//        xlWXZFBtn.layer.masksToBounds = YES;
//        xlWXZFBtn.layer.cornerRadius = 4;
//        [xlWXZFBtn setTitle:@"微信支付" forState:UIControlStateNormal];
//        [xlWXZFBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
//        [xlWXZFBtn setImage:[UIImage imageNamed:@"logo_weixin"] forState:UIControlStateNormal];
//        [xlWXZFBtn addTarget:self action:@selector(xlwxZFAction:) forControlEvents:UIControlEventTouchUpInside];
//        [bottomView addSubview:xlWXZFBtn];
//
//
//        UIButton * xlZFBZFBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(xlWXZFBtn.frame)+xj,xlWXZFBtn.frame.origin.y,130,36)];
//        xlZFBZFBtn.backgroundColor = RGB_HEX(0x00a9f2);
//        xlZFBZFBtn.layer.masksToBounds = YES;
//        xlZFBZFBtn.layer.cornerRadius = 4;
//        [xlZFBZFBtn setTitle:@"支付宝支付" forState:UIControlStateNormal];
//        [xlZFBZFBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
//        [xlZFBZFBtn setImage:[UIImage imageNamed:@"logo_zhifbao"] forState:UIControlStateNormal];
//        [xlZFBZFBtn addTarget:self action:@selector(xlZFBAction:) forControlEvents:UIControlEventTouchUpInside];
//        [bottomView addSubview:xlZFBZFBtn];
//    }
//
    
}

-(void)chooseMoney:(UIButton *)btn
{
    UIView * firstMoneyView =(UIView *)[self viewWithTag:101];
    UIView * secondMoneyView =(UIView *)[self viewWithTag:201];
    UIImageView * icon1 = (UIImageView *)[self viewWithTag:102];
    UIImageView * icon2 = (UIImageView *)[self viewWithTag:202];
    UILabel * firstLabelTop = (UILabel *)[self viewWithTag:103];
    UILabel * firstLabelBottom = (UILabel *)[self viewWithTag:104];
    UILabel * secondLabelTop = (UILabel *)[self viewWithTag:203];
    UILabel * secondLabelBottom = (UILabel *)[self viewWithTag:204];
    if(btn.tag == 105)
    {
        icon1.hidden = NO;
        icon2.hidden = YES;
        firstLabelTop.textColor = RGB_HEX(0xFFBB01);
        firstLabelBottom.textColor = RGB_HEX(0xFFBB01);
        secondLabelTop.textColor = RGB_HEX(0x666666);
        secondLabelBottom.textColor = RGB_HEX(0x666666);
        self.money = @"0.1";
        firstMoneyView.layer.borderColor = RGB_HEX(0xFFBB01).CGColor;
        secondMoneyView.layer.borderColor = RGB_HEX(0xcccccc).CGColor;
    }else
    {
        icon2.hidden = NO;
        icon1.hidden = YES;
        secondLabelTop.textColor = RGB_HEX(0xFFBB01);
        secondLabelBottom.textColor = RGB_HEX(0xFFBB01);
        firstLabelTop.textColor = RGB_HEX(0x666666);
        firstLabelBottom.textColor = RGB_HEX(0x666666);
        self.money = @"99";
        secondMoneyView.layer.borderColor = RGB_HEX(0xFFBB01).CGColor;
        firstMoneyView.layer.borderColor = RGB_HEX(0xcccccc).CGColor;
    }
}


//关闭
-(void)handelCloseAction:(UIButton *)closeBtn
{
    
    [self popHiden];
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}

-(void)testTopupMoney
{
    if ([self.delegate respondsToSelector:@selector(XLNewtestTopup)]) {
        
        [self.delegate XLNewtestTopup];
    }
    
}

//AppstorePay
-(void)payBtn:(UIButton *)appstorePay
{
    if ([self.delegate respondsToSelector:@selector(xlAppstorePay:)]) {
        
        [self.delegate xlAppstorePay:appstorePay];
    }
    
    
}

//选择金豆
-(void)selecttedJDAction:(UIButton *)jdSeleBtn
{
    
    jdSeleBtn.selected = !jdSeleBtn.selected;
    self.Selectbutton.selected = !self.Selectbutton.selected;
    
    UILabel * titleLab1 = (UILabel *)[jdSeleBtn viewWithTag:jdSeleBtn.tag+10];
    UILabel * jdLab1    = (UILabel *)[jdSeleBtn viewWithTag:jdSeleBtn.tag+20];
    titleLab1.textColor = RGB_HEX(0xff4a45);
    jdLab1.textColor    = RGB_HEX(0xff4a45);
    
    UILabel * titleLab = (UILabel *)[self.Selectbutton viewWithTag:self.Selectbutton.tag+10];
    UILabel * jdLab    = (UILabel *)[self.Selectbutton viewWithTag:self.Selectbutton.tag+20];
    
    titleLab.textColor = RGB_HEX(0x272727);
    jdLab.textColor = RGB_HEX(0x999999);
    
    self.Selectbutton = jdSeleBtn;


    if ([self.delegate respondsToSelector:@selector(XLSelectedJD:)]) {
        
        [self.delegate XLSelectedJD:jdSeleBtn];
        
    }
    
    
}

//微信支付
-(void)xlwxZFAction:(UIButton *)wxbtn
{
    [self popHiden];
    if ([self.delegate respondsToSelector:@selector(XLNewWXZHIFU:)]) {
        
        [self.delegate XLNewWXZHIFU:wxbtn];
    }
    
}

//支付宝支付
-(void)xlZFBAction:(UIButton *)zfbbtn
{
    [self popHiden]; 
    if ([self.delegate respondsToSelector:@selector(XLNewZFBZHIFU:)]) {
        
        [self.delegate XLNewZFBZHIFU:zfbbtn];
    }
    
}


//关闭视图
- (void)popHiden {
    
    self.hidden = YES;
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(topupViewDidDisAppear)]) {
        [self.delegate topupViewDidDisAppear];
    }
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [self popHiden];
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
